package hc.bre

import static org.junit.jupiter.api.Assertions.*

import hc.bre.clas.yesno.*
import java.lang.reflect.Field
import org.junit.jupiter.api.Test

class RelectionCallTest {
	int NoId = 0;
	int YesId = 1;
	@Test
	void buildAndRelection1CallNoContext() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='false'

		Class clazz = ruleDef.buildRuleClass()

		Field field = clazz.getField("rule");
		Closure rule = field.get(clazz);
		assert rule.call().clas.id == NoId
	}

	@Test
	void buildAndRelection2CallNoContext() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='false'

		Class clazz = ruleDef.buildRuleClass()
		def ret = clazz.invokeMethod('rule', null);
		assert ret.clas.id == NoId
	}

	@Test
	void buildAndRelectionCallWithObjectContext() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='variable > 8;'

		Class clazz = ruleDef.buildRuleClass()

		Field field = clazz.getField("rule");
		Closure rule = field.get(clazz);

		Closure withrule = rule.clone()
		withrule.delegate = new Object() {
					def variable = 9;
				};

		assert withrule.call().clas.id == YesId
	}

	@Test
	void buildAndRelectionCallWithMapContext() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='variable > 8;'

		Class clazz = ruleDef.buildRuleClass()

		Field field = clazz.getField("rule");
		Closure rule = field.get(clazz);

		Closure withrule = rule.clone()
		HashMap map = new HashMap()
		map.put("variable", 9);
		withrule.delegate = map;

		assert withrule.call().clas.id == YesId
	}
	@Test
	void buildAndRelectionCallWithGroovyLangScript() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='variable > 8;'

		Class clazz = ruleDef.buildRuleClass()

		Field field = clazz.getField("rule");
		Closure rule = field.get(clazz);

		Closure withrule = rule.clone()

		withrule.delegate = new groovy.lang.Script() {
					@Override
					public Object run() {
						return null;
					}
				};

		withrule.delegate.variable = 9;

		assert withrule.call().clas.id == YesId
	}

	@Test
	void buildAndRelectionCallWithGroovyLangScript2() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='variable > 8;'

		Class clazz = ruleDef.buildRuleClass()

		Field field = clazz.getField("rule");
		Closure rule = field.get(clazz);

		Closure withrule = rule.clone()

		withrule.delegate = new groovy.lang.Script() {
					def variable = 9;
					@Override
					public Object run() {
						return null;
					}
				};
		assert withrule.call().clas.id == YesId
	}

	@Test
	void buildAndRelectionCallWithGroovyLangScript3() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='variable > 8;'

		Class clazz = ruleDef.buildRuleClass()

		Field field = clazz.getField("rule");
		Closure rule = field.get(clazz);

		Closure withrule = rule.clone()
		Binding binding = new Binding();;
		binding.setProperty('variable', 9)
		withrule.delegate = new groovy.lang.Script(binding) {
					@Override
					public Object run() {
						return null;
					}
				};
		assert withrule.call().clas.id == YesId
	}

//	@Test TEMPXYZ
	void buildAndRelectionCallWithExtClass() {
		YesNoDefinition ruleDef = YesNoDefinition.create();
		ruleDef.rpath = "prj/Hello"
		ruleDef.script ='with(xsxdjc.class9.是否优质画像.rule).clas.id == 1;'

		Class clazz = ruleDef.buildRuleClass()

		Field field = clazz.getField("rule");
		Closure rule = field.get(clazz);
		Closure withrule = rule.clone()
		withrule.delegate = new groovy.lang.Script() {
					def 基本信息 = [教育:'本科'];
					@Override
					public Object run() {
						return null;
					}
				};
		def ret = withrule.call();

		assert ret.clas.id == YesId;
	}
}
